/************************************************************************
                                                                     
   Xceed Ultimate ListBox for Silverlight                                                                                                                                            
   Copyright (C) 2010 Xceed Software Inc.    
                                                                     
   This program is provided to you under the terms of the GNU General Public  
   License version 2 as published by the Free Software Foundation. 
        
   This program is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY, without even the implied warranty of 
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License along 
   with this program, if not, write to the Free Software Foundation, Inc., 
   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

   This program can be provided to you by Xceed Software Inc. under a
   proprietary commercial license agreement for use in non-Open Source
   projects. Visit Xceed.com to find the commercial edition of 
   Xceed Ultimate ListBox for Silverlight.                                    
                                                                      
 **********************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;

namespace Xceed.Silverlight.Data
{
  internal sealed class FilterExpressionSimplifier : FilterExpressionVisitor
  {
    #region Constructor

    internal FilterExpressionSimplifier( FilterExpressionBase expression )
      : base( expression )
    {
    }

    #endregion

    protected override void Initialize()
    {
      m_result = null;
    }

    protected internal override void Visit( FilterExpression expression )
    {
      m_result = expression;
    }

    protected internal override void Visit( AndFilterExpression expression )
    {
      ICollection<FilterExpressionBase> childExpressions;

      // No simplification has been done.
      if( !this.TryGetNewChildExpressions( expression, out childExpressions ) )
      {
        m_result = expression;
      }
      // A FilterExpressionOperator is valid only if there are at least 
      // two child expressions.
      else if( childExpressions.Count >= 2 )
      {
        m_result = new AndFilterExpression( childExpressions );
      }
      else
      {
        m_result = childExpressions.Single();
      }
    }

    protected internal override void Visit( OrFilterExpression expression )
    {
      ICollection<FilterExpressionBase> childExpressions;

      // No simplification has been done.
      if( !this.TryGetNewChildExpressions( expression, out childExpressions ) )
      {
        m_result = expression;
      }
      // A FilterExpressionOperator is valid only if there are at least 
      // two child expressions.
      else if( childExpressions.Count >= 2 )
      {
        m_result = new OrFilterExpression( childExpressions );
      }
      else
      {
        m_result = childExpressions.Single();
      }
    }

    protected internal override void Visit( NotFilterExpression expression )
    {
      NotFilterExpression childExpression = expression.ChildExpression as NotFilterExpression;

      // Two consecutive "Not" may be simplified.
      if( childExpression != null )
      {
        this.Visit( childExpression.ChildExpression );
      }
      // Try to simplify the child expression itself.
      else
      {
        this.Visit( expression.ChildExpression );

        // No simplification has been done.
        if( m_result == expression.ChildExpression )
        {
          m_result = expression;
        }
        else
        {
          m_result = new NotFilterExpression( m_result );
        }
      }
    }

    internal FilterExpressionBase GetResult()
    {
      this.Visit();

      return m_result;
    }

    private bool TryGetNewChildExpressions( FilterExpressionOperator expression, out ICollection<FilterExpressionBase> childExpressions )
    {
      Type expressionType = expression.GetType();
      List<FilterExpressionBase> newChildExpressions = new List<FilterExpressionBase>( expression.ChildExpressions.Count );
      bool hasChanged = false;

      foreach( FilterExpressionBase childExpression in expression.ChildExpressions )
      {
        this.Visit( childExpression );

        FilterExpressionBase newChildExpression = m_result;

        // The child expression is of the same operator type.  The children of the child
        // expression may be promoted to this level.
        if( expressionType == newChildExpression.GetType() )
        {
          FilterExpressionOperator childOperator = ( FilterExpressionOperator )newChildExpression;

          foreach( FilterExpressionBase promotedChildExpression in childOperator.ChildExpressions )
          {
            if( !newChildExpressions.Contains( promotedChildExpression ) )
            {
              newChildExpressions.Add( promotedChildExpression );
            }
          }

          hasChanged = true;
        }
        // Make sure the child expression isn't a duplicate of an expression we
        // have already managed.
        else if( !newChildExpressions.Contains( newChildExpression ) )
        {
          newChildExpressions.Add( newChildExpression );

          hasChanged |= ( newChildExpression != childExpression );
        }
        // The child expression is a duplicate.  We may ignore it.
        else
        {
          hasChanged = true;
        }
      }

      if( hasChanged )
      {
        childExpressions = newChildExpressions;
      }
      else
      {
        childExpressions = expression.ChildExpressions;
      }

      return hasChanged;
    }

    #region Private Fields

    private FilterExpressionBase m_result;

    #endregion
  }
}
